package com.ambimmort.shj.service.policy;

import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.user.operation.UserPolicyAction;
import com.ambimmort.prserver.bean.RepositoryOperationLogBean;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Josh on 2015/7/31.
 */
@Service
public class PolicyService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;

    public void create(String username,String type,String instance,String policy,String comment)  throws Throwable{

        PolicySupport.getInstance().getApi().addPolicy(type, instance, policy, comment);
        UserPolicyAction.createPolicy(username, instance, PolicySupport.getInstance().getApi().getHeadVersion(type, instance), type);  //创建用户创建策略日志

    }

    public void delete(String username,String type,String instance,String messageNo)throws Throwable{

        RepositoryOperationLogBean bean = PolicySupport.getInstance().getApi().deletePolicy(type, instance, Integer.parseInt(messageNo));
        UserPolicyAction.deletePolicy(username, instance, Long.parseLong(messageNo), PolicySupport.getInstance().getApi().getHeadVersion(type, instance), type);

    }

    public String get(String type,String instance,String messageNo) {

        RepositoryOperationLogBean bean = PolicySupport.getInstance().getApi().getMessage(type, instance, Integer.parseInt(messageNo));
        JSONObject obj = new JSONObject();
        obj.put("MessageNo", bean.getSvnFile().getMessageNo() + "");
        obj.put("MessageType", type);
        obj.put("Instance", instance);
        JSON json = JSONObject.fromObject(bean.getSvnFile().getContent());
        obj.put("Content", json);
        obj.put("Comment", bean.getSvnFile().getComment());
        return obj.toString();

    }

    public String historyPolicyInfo(String type, byte[] instanceArr, String messageNo) throws UnsupportedEncodingException {

        String instance = new String(instanceArr, "utf-8");

        List<RepositoryOperationLogBean> beans = PolicySupport.getInstance().getApi().getMessageLogs(type, instance, Integer.parseInt(messageNo));
        Map<Long, String> strMap;  //通过ID来进行排序 也就是这个MessageNo的版本号  默认的是按照升序排列
        strMap = new TreeMap<Long, String>();

        for (RepositoryOperationLogBean bean : beans) {
            JSONObject obj = new JSONObject();
            obj.put("MessageNo", bean.getSvnFile().getMessageNo() + "");
            obj.put("MessageType", type);
            obj.put("Instance", instance);
            obj.put("MessageSerialNo", bean.getId());  //策略的版本号
            obj.put("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(bean.getCreateTime())));
            JSON json = JSONObject.fromObject(bean.getSvnFile().getContent());
            obj.put("Content", json);
            obj.put("Comment", bean.getSvnFile().getComment());
            strMap.put(bean.getId(), obj.toString());

        }
        JSONArray jor = new JSONArray();
        for (long key : strMap.keySet()) {
            jor.add(strMap.get(key));
        }
          /* TODO output your page here. You may use following sample code. */
        return jor.toString();
    }

    public String list(String type, String instance) {
        java.util.List<RepositoryOperationLogBean> ls = PolicySupport.getInstance().getApi().getHeadPoliciesWithLog(type, instance, 1000, 0);

        JSONArray array = new JSONArray();

        for (RepositoryOperationLogBean bean : ls) {
            JSONObject obj = new JSONObject();
            obj.put("Description", bean.getSvnFile().getComment());
            obj.put("MessageNo", bean.getSvnFile().getMessageNo());
            obj.put("Version", bean.getId() + "");
            array.add(obj);

        }return array.toString();
    }

    public void update(String username,String type,String instance,String messageNo,String policy,String comment)  throws Throwable{

        PolicySupport.getInstance().getApi().updatePolicy(type, instance, Integer.parseInt(messageNo), policy, comment);
        UserPolicyAction.editPolicy(username, instance, Long.parseLong(messageNo), PolicySupport.getInstance().getApi().getHeadVersion(type, instance), type);

    }
}
